package com.hongxun.budget.ui.fragment.sg;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.sg.ConstructionTwoActivity;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.StringUtils;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.entity.sg.CZjjc;
import com.hongxun.greenentity.sg.CPBaseEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 中间检查
 */
public class MiddleCheck extends BaseListFragment {
    private SuperAdapter<CZjjc> superAdapter;
    private List<CZjjc> datas = new ArrayList<>();

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getLinearLayoutManagerForVertical(context);
    }

    @Override
    public SuperAdapter getAdapter() {
        superAdapter = new SuperAdapter<CZjjc>(R.layout.sg_content_mc_adapter) {

            @Override
            public void bindView(ViewHolder viewHolder, final CZjjc itemBean, int i) {
                try {
                    ImageView imageView = viewHolder.getView(R.id.iv1);
                    XUtils.loadImg(context, itemBean.getUrl(), imageView);
                    ((TextView) viewHolder.getView(R.id.tv1)).setText(StringUtils.getNull(itemBean.getXmName()));
                    ((TextView) viewHolder.getView(R.id.tv2)).setText(itemBean.getSgzjCount() + "");
                    ((TextView) viewHolder.getView(R.id.tv4)).setText(itemBean.getXczjCount() + "");
                } catch (NullPointerException e) {
                }
            }
        };
        superAdapter.setData(datas);
        superAdapter.setEmptyDataView(R.layout.view_empty);
        superAdapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<CZjjc>() {
            @Override
            public void onClick(int i, CZjjc xmList) {
                SPUtils.put(context, SgStrKey.uuidxm, xmList.getUuidXm());
                SPUtils.put(context, SgStrKey.namexm, xmList.getXmName());
                startActivity(new Intent(context, ConstructionTwoActivity.class)
                        .putExtra("tabName", SgStrKey.zjjclist));
            }
        });
        return superAdapter;
    }

    @Override
    public void onRefresh() {
        datas.clear();
        Observable.just(SgStrKey.uuidjl).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String value) {
                List<CPBaseEntity> listPro = SgDBUtils.getCPBaseList(false);
                for (CPBaseEntity entity : listPro) {
                    CZjjc zjjc = new CZjjc();
                    zjjc.setUrl(SgDBUtils.get123Zp(value, entity.getGuid()));
                    zjjc.setXmName(entity.getPBName());
                    zjjc.setUuidXm(entity.getGuid());
                    zjjc.setSgzjCount(SgDBUtils.getZjjcCount(entity.getGuid(), "100"));
                    zjjc.setXczjCount(SgDBUtils.getZjjcCount(entity.getGuid(), "200"));
                    datas.add(zjjc);
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }

    //    @Override
//    public void loadMore(int loadPage, LoadDataStatus loadDataStatus) {
//        super.loadDataStatus = loadDataStatus;
//        if (datas.isEmpty()){
//            handler.sendEmptyMessageDelayed(4, 1000);
//            return;
//        }
//        if (datas.size() >= 20) {
//            handler.sendEmptyMessageDelayed(3, 1000);
//            return;
//        }
//        List<MaterialPresent> data00 = new ArrayList<>();
//        for (int i = datas.size(); i < datas.size() + 6; i++) {
//            MaterialPresent bean = new MaterialPresent();
//            bean.setCount(i + "");
//            bean.setProName("凤冈县201" + i + "年度10kV天小线低电压整治工");
//            bean.setUrl("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3321274202,2635726987&fm=27&gp=0.jpg");
//            data00.add(bean);
//        }
//        Message msg = handler.obtainMessage();
//        msg.what = 2;
//        msg.obj = data00;
//        handler.sendMessageDelayed(msg, 1000);
//    }
}
